﻿using System.Data.Common;

namespace KrasDD.Extensions
{
    public static class DbDataReaderExtensions
    {
        public static Dictionary<string, object> GetDictionary(this DbDataReader? dataReader)
        {
            if (dataReader == null || !dataReader.HasRows)
            {
                return new Dictionary<string, object>();
            }
            var colInfos = dataReader.GetColumnSchema();
            var obj = new Dictionary<string, object>();
            for (int i = 0; i < dataReader.FieldCount; i++)
            {
                var colName = dataReader.GetName(i).ToLower();
                var value = dataReader.GetValue(i);
                // 判断列类型是否为char(1)
                if (colInfos[i].DataTypeName == "char" && colInfos[i].ColumnSize == 1)
                {
                    if (value.ToString() == "1" || value.ToString().ToLower() == "y" || value.ToString().ToLower() == "t")
                    {
                        value = true;
                    }
                    else
                    {
                        value = false;
                    }
                }
                obj.Add(colName, value);
            }
            return obj;
        }
    }
}
